package com.myapp.ruyixing.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.Model;

public class EmailRegisterFragment extends Fragment implements OnClickListener {
	private void showToast(String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
	}

	private void showLoading(boolean show, String msg) {
		Activity activity = getActivity();
		IShowLoading showLoading = null;
		try {
			showLoading = (IShowLoading) activity;
		} catch (Exception e) {
			return;
		}
		showLoading.showLoading(show, msg);
	}

	public EmailRegisterFragment() {
		// Required empty public constructor
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		return inflater.inflate(R.layout.fragment_register_email, container,
				false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		view.findViewById(R.id.text_agree).setOnClickListener(this);
		view.findViewById(R.id.check_agree).setOnClickListener(this);
		view.findViewById(R.id.btn_commit).setOnClickListener(this);
		super.onViewCreated(view, savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.check_agree:
			checkCommitButtonEnable();
			break;
		case R.id.btn_commit:
			String email = ((EditText) getView().findViewById(R.id.edit_email))
					.getText().toString();
			if (TextUtils.isEmpty(email)) {
				showToast("请输入电子邮件！");
				return;
			}
			String username = ((EditText) getView().findViewById(
					R.id.edit_nickname)).getText().toString();
			if (TextUtils.isEmpty(username)) {
				showToast("请输入昵称！");
				return;
			}
			String password = ((EditText) getView().findViewById(
					R.id.edit_password)).getText().toString();
			if (TextUtils.isEmpty(password)) {
				showToast("请输入密码！");
				return;
			}
			// String icode = ((EditText)
			// getView().findViewById(R.id.edit_verify))
			// .getText().toString();
			// if (TextUtils.isEmpty(icode)) {
			// showToast("请输入验证码！");
			// return;
			// }
			if (!((CheckBox) getView().findViewById(R.id.check_agree))
					.isChecked()) {
				showToast("请阅读协议后勾选同意！");
				return;
			}
			register(email, username, password);
			break;
		case R.id.text_agree:
			intent = new Intent(getActivity(), AgreementActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
	}

	private void register(String email, String username, String password) {
		showLoading(true, "加载中");
		RYHttpClient.registerByEmail(getActivity(),
				new RYHttpResponseHandler() {

					@Override
					public void onSuccess(Object o) {
						try {
							Model model = (Model) o;
							onRegisterFinished(model);
						} catch (Exception e) {
							onRegisterFinished(null);
						}
					}

					@Override
					public void onFailure(Throwable throwable) {
						onRegisterFinished(null);
					}
				}, email, username, password);
	}

	private void onRegisterFinished(Model model) {
		showLoading(false, "加载中");
		if (model != null && model.errno == 0) {
			showToast("注册成功，请登录！");
			getActivity().finish();
			return;
		}
		showToast("注册失败：" + (model == null ? "" : model.msg));
	}

	private void checkCommitButtonEnable() {

	}
}
